package br.ucb.ProjetoFinal.validators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 * source : www.javabeat.net
 */
public class ValidatorCodigoApf implements Validator{

    public void validate(FacesContext facesContext, UIComponent arg1, Object value) throws ValidatorException {

        String cod = (String) value;

        Pattern mascara = Pattern.compile("[0-9]{7}[-][0-9]{2}");
        Matcher match = mascara.matcher(cod);

        if (!match.matches() && !cod.equals("")){
               FacesMessage message = new FacesMessage();
               message.setSummary("Codigo APF - Codigo inválido. Ele deve estar no formato xxxxxxx-xx'");
               message.setSeverity(FacesMessage.SEVERITY_ERROR);
               throw new ValidatorException(message);            
        }
    }
}